home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint108s.zoo / signal.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-27  |  16.6 KB  |  627 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1992,1993 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* signal.c:: signal handling routines */
  8.  
  9. #include "mint.h"
  10.  
  11. void (*sig_routine)();    /* used in intr.s */
  12. short sig_exc;        /* used in intr.s */
  13.  
  14. /*
  15.  * killgroup(pgrp, sig): send a signal to all members of a process group
  16.  * returns 0 on success, or an error code on failure
  17.  */
  18.  
  19. long
  20. killgroup(pgrp, sig)
  21.     int pgrp, sig;
  22. {
  23.     PROC *p;
  24.     int found = 0;
  25.  
  26.     TRACE(("killgroup %d %d", pgrp, sig));
  27.  
  28.     if (pgrp < 0)
  29.         return EINTRN;
  30.  
  31.     for (p = proclist; p; p = p->gl_next) {
  32.         if (p->pgrp == pgrp) {
  33.             post_sig(p, sig);
  34.             found++;
  35.         }
  36.     }
  37.     if (found) {
  38.         check_sigs();    /* see if the current process is affected */
  39.         return 0;
  40.     }
  41.     else {
  42.         DEBUG(("killgroup: no processes found"));
  43.         return EFILNF;
  44.     }
  45. }
  46.  
  47. /* post_sig: post a signal as being pending. It is assumed that the
  48.    caller has already verified that "sig" is a valid signal, and
  49.    moreover it is the caller's responsibility to call check_sigs()
  50.    if it's possible that p == curproc
  51.  */
  52.  
  53. void
  54. post_sig(p, sig)
  55.     PROC *p;
  56.     int sig;
  57. {
  58.     ulong sigm;
  59.  
  60. /* if process is ignoring this signal, do nothing
  61.  * also: signal 0 is SIGNULL, and should never be delivered through
  62.  * the normal channels (indeed, it's filtered out in dossig.c,
  63.  * but the extra sanity check here is harmless). The kernel uses
  64.  * signal 0 internally for some purposes, but it is handled
  65.  * specially (see supexec() in xbios.c, for example).
  66.  */
  67.     if (p->sighandle[sig] == SIG_IGN || sig == 0)
  68.         return;
  69.  
  70. /* if the process is already dead, do nothing */
  71.     if (p->wait_q == ZOMBIE_Q || p->wait_q == TSR_Q)
  72.         return;
  73.  
  74. /* mark the signal as pending */
  75.     sigm = (1L << (unsigned long)sig);
  76.     p->sigpending |= sigm;
  77.  
  78. /* if the signal is masked, do nothing further */
  79. /* note: some signals can't be masked, and we handle those elsewhere so
  80.  * that p->sigmask is always valid. SIGCONT is among the unmaskable
  81.  * signals
  82.  */
  83.     if ( (p->sigmask & sigm) != 0 )
  84.         return;
  85.  
  86. /* otherwise, make sure the process is awake */
  87.     if (p->wait_q && p->wait_q != READY_Q) {
  88.         short sr = spl7();
  89.         if (p->wait_q == SELECT_Q)
  90.             p->wait_cond = 0;
  91.         rm_q(p->wait_q, p);
  92.         add_q(READY_Q, p);
  93.         spl(sr);
  94.     }
  95. }
  96.  
  97. /*
  98.  * check_sigs: see if we have any signals pending. if so,
  99.  * handle them.
  100.  */
  101.  
  102. void
  103. check_sigs()
  104. {
  105.     ulong sigs, sigm;
  106.     int i;
  107.     short deliversig;
  108.  
  109.     if (curproc->pid == 0) return;
  110. top:
  111.     sigs = curproc->sigpending & ~(curproc->sigmask);
  112.     if (sigs) {
  113.         sigm = 2;
  114. /* with tracing we need a mechanism to allow a signal to be delivered
  115.  * to the child (curproc); Fcntl(...TRACEGO...) passes a SIGNULL to indicate that we
  116.  * should really deliver the signal, hence its always safe to remove it
  117.  * from pending.
  118.  */
  119.         deliversig = (curproc->sigpending & 1L);
  120.         curproc->sigpending &= ~1L;
  121.  
  122.         for (i = 1; i < NSIG; i++) {
  123.             if (sigs & sigm) {
  124.                 curproc->sigpending &= ~sigm;
  125.                 if (curproc->ptracer && !deliversig &&
  126.                     i != SIGCONT) {
  127.                     TRACE(("tracer being notified of signal %d", i));
  128.                     stop(i);
  129.         /* the parent may reset our pending signals, so check again */
  130.                     goto top;
  131.                 } else {
  132.                     ulong omask;
  133.  
  134.                     curproc->sigpending &= ~sigm;
  135.                     omask = curproc->sigmask;
  136.  
  137.         /* sigextra gives which extra signals should also be masked */
  138.                     curproc->sigmask |= curproc->sigextra[i] | sigm;
  139.                     handle_sig(i);
  140.  
  141.  
  142. /*
  143.  * POSIX.1-3.3.4.2(723) "If and when the user's signal handler returns
  144.  * normally, the original signal mask is restored."
  145.  *
  146.  * BUG?: This unmasking could unmask a pending signal which we will not
  147.  * see this time around (if the signal number is less than i) and which
  148.  * was not pending when we started; should we detect this condition and
  149.  * loop around for a second try? POSIX only guarantees delivery of
  150.  * one signal per kernel entry, so this shouldn't really be a problem.
  151.  */
  152.                     curproc->sigmask = omask;    /* unmask signals */
  153.                 }
  154.             }
  155.             sigm = sigm << 1;
  156.         }
  157.     }
  158. }
  159.  
  160. /*
  161.  * raise: cause a signal to be raised in the current process
  162.  */
  163.  
  164. void
  165. raise(sig)
  166.     int sig;
  167. {
  168.     post_sig(curproc, sig);
  169.     check_sigs();
  170. }
  171.  
  172. #ifdef EXCEPTION_SIGS
  173. /* exception numbers corresponding to signals */
  174. char excep_num[NSIG] =
  175. { 0, 0, 0, 0,
  176.   4,            /* SIGILL == illegal instruction */
  177.   9,            /* SIGTRAP == trace trap    */
  178.   4,            /* pretend SIGABRT is also illegal instruction */
  179.   8,            /* SIGPRIV == privileged instruction exception */
  180.   5,            /* SIGFPE == divide by zero */
  181.   0, 2,            /* SIGBUS == bus error */
  182.   3            /* SIGSEGV == address error */
  183. /* everything else gets zeros */
  184. };
  185.  
  186. /* a "0" means we don't print a message when it happens -- typically the
  187.    user is expecting a synchronous signal, so we don't need to report it
  188. */
  189.  
  190. const char *signames[NSIG] = { 0,
  191. 0, 0, 0, "ILLEGAL INSTRUCTION", "TRACE TRAP",
  192. 0, "PRIVILEGE VIOLATION", "DIVISION BY ZERO", 0, "BUS ERROR",
  193. "ADDRESS ERROR", "BAD SYSTEM CALL", 0, 0, 0,
  194. 0, 0, 0, 0, 0,
  195. 0, 0, 0, "CPU TIME EXHAUSTED", "FILE TOO BIG",
  196. 0, 0, 0, 0, 0
  197. };
  198.  
  199. /*
  200.  * replaces the TOS "show bombs" routine: for now, print the name of the
  201.  * interrupt on the console, and save info on the crash in the appropriate
  202.  * system area
  203.  */
  204.  
  205. void
  206. bombs(sig)
  207.     int sig;
  208. {
  209.     long *procinfo = (long *)0x380L;
  210.     int i;
  211.     CONTEXT *crash;
  212.     extern int no_mem_prot;
  213.  
  214.     if (sig < 0 || sig > 31) {
  215.         ALERT("bombs(%d): sig out of range", sig);
  216.     }
  217.     else if (signames[sig]) {
  218.         if (!no_mem_prot && sig == SIGBUS) {
  219.             /* already reported by report_buserr */
  220.         } else {
  221.             ALERT("%s: User PC=%lx (basepage=%lx)",
  222.                 signames[sig],
  223.                 curproc->exception_pc, curproc->base);
  224.         }
  225. /* save the processor state at crash time */
  226. /* assumes that "crash time" is the context curproc->ctxt[SYSCALL] */
  227. /* BUG: this is not true if the crash happened in the kernel; in the
  228.  * latter case, the crash context wasn't saved anywhere.
  229.  */
  230.         crash = &curproc->ctxt[SYSCALL];
  231.         *procinfo++ = 0x12345678L;    /* magic flag for valid info */
  232.         for (i = 0; i < 15; i++)
  233.             *procinfo++ = crash->regs[i];
  234.         *procinfo++ = curproc->exception_ssp;
  235.         *procinfo++ = ((long)excep_num[sig]) << 24L;
  236.         *procinfo = crash->usp;
  237.  
  238. /* we're also supposed to save some info from the supervisor stack. it's not
  239.  * clear what we should do for MiNT, since most of the stuff that used to be
  240.  * on the stack has been put in the CONTXT struct. Moreover, we don't want
  241.  * to crash because of an attempt to access illegal memory. Hence, we do
  242.  * nothing here...
  243.  */
  244.     } else {
  245.         TRACE(("bombs(%d)", sig));
  246.     }
  247. }
  248. #endif
  249.  
  250. /*
  251.  * handle_sig: do whatever is appropriate to handle a signal
  252.  */
  253.  
  254. static long unwound_stack = 0;
  255.  
  256. void
  257. handle_sig(sig)
  258.     int sig;
  259. {
  260.     long oldstack, newstack;
  261.     long *stack;
  262.     CONTEXT *call, contexts[2];
  263. #define oldsysctxt (contexts[0])
  264. #define newcurrent (contexts[1])
  265.  
  266.     extern void sig_return();
  267.  
  268.     if (curproc->sighandle[sig] == SIG_IGN)
  269.         return;
  270.     else if (curproc->sighandle[sig] == SIG_DFL) {
  271. _default:
  272.         switch(sig) {
  273. #if 0
  274. /* Note: SIGNULL is filtered out in dossig.c and is never actually
  275.  * delivered (its only purpose for the user is to test for the existence of
  276.  * a process, it isn't a real signal). The kernel uses SIGNULL
  277.  * internally, but all such code does the signal handling "by hand"
  278.  * and so no default handling is necessary.
  279.  */
  280.         case SIGNULL:
  281. #endif
  282.         case SIGWINCH:
  283.         case SIGCHLD:
  284. /* SIGFPE is divide by 0; TOS ignores this, so we will too */
  285.         case SIGFPE:
  286.             return;        /* do nothing */
  287.         case SIGSTOP:
  288.         case SIGTSTP:
  289.         case SIGTTIN:
  290.         case SIGTTOU:
  291.             stop(sig);
  292.             return;
  293.         case SIGCONT:
  294.             curproc->sigpending &= ~STOPSIGS;
  295.             return;
  296.  
  297. /* here are the fatal signals. for SIGINT, we use p_term(-32) so that
  298.  * TOS programs that catch ^C via the vector at 0x400 and which expect
  299.  * TOS's error code (-32) to be sent will work. For most other signals,
  300.  * we p_term with an error code; for SIGKILL, we don't want to allow
  301.  * the program any chance to recover, so we call terminate() directly
  302.  * to avoid calling through to the user's terminate vector.
  303.  */
  304.         case SIGINT:        /* ^C */
  305.             if (curproc->domain == DOM_TOS) {
  306.                 p_term(-32);
  307.                 return;
  308.             }
  309.             /* otherwise, fall through */
  310.         default:
  311. #ifdef EXCEPTION_SIGS
  312.             bombs(sig); /* tell the user what happened */
  313. #endif
  314.     /* the "sigmask" check is in case a bus error happens in the user's
  315.      * term_vec code; we don't want to get stuck in an infinite loop!
  316.      */
  317.             if ((curproc->sigmask & 1L) || sig == SIGKILL)
  318.                 terminate(sig << 8, ZOMBIE_Q);
  319.             else
  320.                 p_term(sig << 8);
  321.         }
  322.     }
  323.     else {        /* user wants to handle it himself */
  324.  
  325. /* another kludge: there is one case in which the p_sigreturn mechanism
  326.  * is invoked by the kernel, namely when the user calls Supexec()
  327.  * or when s/he installs a handler for the GEMDOS terminate vector (#0x102)
  328.  * and the program terminates. MiNT fakes the call to user code with
  329.  * signal 0 (SIGNULL); programs that longjmp out of the user function
  330.  * and are later sent back to it again (e.g. if ^C keeps getting pressed
  331.  * and a terminate vector has been installed) will grow the stack without
  332.  * bound unless we watch for this case.
  333.  *
  334.  * Solution (sort of): whenever Pterm() is called, we unwind the
  335.  * stack; otherwise, we let it grow, so that nested Supexec()
  336.  * calls work.
  337.  *
  338.  * Note that SIGNULL is thrown away when sent by user processes, 
  339.  * and the user can't mask it (it's UNMASKABLE), so there is
  340.  * is no possibility of confusion with anything the user does.
  341.  */
  342.         if (sig == 0) {
  343.     /* p_term() sets sigmask to let us know to do Psigreturn */
  344.             if (curproc->sigmask & 1L) {
  345.                 p_sigreturn();
  346.                 curproc->sigmask &= ~1L;
  347.             } else {
  348.                 unwound_stack = 0;
  349.             }
  350.         }
  351.  
  352.         call = &curproc->ctxt[SYSCALL];
  353. /*
  354.  * what we do is build two fake stack frames; the bottom one is
  355.  * for a call to the user function, with (long)parameter being the
  356.  * signal number; the top one is for sig_return.
  357.  * When the user function returns, it returns to sig_return, which
  358.  * calls into the kernel to restore the context in prev_ctxt
  359.  * (thus putting us back here). We can then continue on our way.
  360.  */
  361.  
  362. /* set a new system stack, with a bit of buffer space */
  363.         oldstack = curproc->sysstack;
  364.         newstack = ((long) ( (&newcurrent) - 2 )) - 12;
  365.  
  366.         if (newstack < (long)curproc->stack + ISTKSIZE + 256) {
  367.             ALERT("stack overflow");
  368.             goto _default;
  369.         }
  370.         else if ((long) curproc->stack + STKSIZE < newstack) {
  371.             FATAL("system stack not in proc structure");
  372.         }
  373.  
  374. /* unwound_stack is set by p_sigreturn() */
  375.         if (sig == 0 && unwound_stack)
  376.             curproc->sysstack = unwound_stack;
  377.         else
  378.             curproc->sysstack = newstack;
  379.         oldsysctxt = *call;
  380.         stack = (long *)(call->sr & 0x2000 ? call->ssp :
  381.                 call->usp);
  382. /*
  383.    Hmmm... here's another potential problem for the signal 0 terminate
  384.    vector: if the program keeps returning back to user mode without
  385.    worrying about the supervisor stack, we'll eventually overflow it.
  386.    However, if the program is in supervisor mode itself, then we don't
  387.    want to stomp on its stack. Temporary solution: ignore the problem,
  388.    the stack's only growing 12 bytes at a time.
  389.  */
  390. /*
  391.  * in addition to the signal number we stuff the vector offset on the
  392.  * stack; if the user is interested they can sniff it, if not ignoring
  393.  * it needs no action on their part. Why do we need this? So that a
  394.  * single SIGFPE handler (for example) can discriminate amongst the
  395.  * multiple things which may get thrown its way
  396.  */
  397.         *(--stack) = (long)call->sfmt & 0xfff;
  398.         *(--stack) = (long)sig;
  399.         *(--stack) = (long)sig_return;
  400.         if (call->sr & 0x2000)
  401.             call->ssp = ((long) stack);
  402.         else
  403.             call->usp = ((long) stack);
  404.         call->pc = (long) curproc->sighandle[sig];
  405.         call->sfmt = call->fstate[0] = 0;    /* don't restart FPU communication */
  406.  
  407.         ((long *)curproc->sysstack)[1] = FRAME_MAGIC;
  408.         ((long *)curproc->sysstack)[2] = oldstack;
  409.         ((long *)curproc->sysstack)[3] = sig;
  410.  
  411.         if (curproc->sigflags[sig] & SA_RESET) {
  412.             curproc->sighandle[sig] = SIG_DFL;
  413.             curproc->sigflags[sig] &= ~SA_RESET;
  414.         }
  415.             
  416.         if (save_context(&newcurrent) == 0 ) {
  417. /*
  418.  * go do the signal; eventually, we'll restore this context (unless the
  419.  * user longjmp'd out of his signal handler). while the user is handling
  420.  * the signal, it's masked out to prevent race conditions. p_sigreturn()
  421.  * will unmask it for us when the user is finished.
  422.  */
  423.             newcurrent.regs[0] = CTXT_MAGIC;
  424.                 /* set D0 so next return is different */
  425.             assert(curproc->magic == CTXT_MAGIC);
  426.             leave_kernel();
  427.             restore_context(call);
  428.         }
  429. /*
  430.  * OK, we get here from p_sigreturn, via the user returning from
  431.  * the handler to sig_return. Restoring the stack and unmasking the
  432.  * signal have been done already for us by p_sigreturn.
  433.  * We should just restore the old system call context
  434.  * and continue with whatever it was we were doing.
  435.  */
  436.         TRACE(("done handling signal"));
  437.         curproc->ctxt[SYSCALL] = oldsysctxt;
  438.         assert(curproc->magic == CTXT_MAGIC);
  439.     }
  440. #undef oldsysctxt
  441. #undef newcurrent
  442. }
  443.  
  444. /*
  445.  * the p_sigreturn system call
  446.  * When called by the user from inside a signal handler, it indicates a
  447.  * desire to restore the old stack frame prior to a longjmp() out of
  448.  * the handler.
  449.  * When called from the sig_return module, it indicates that the user
  450.  * is finished a handler, and we should not only restore the stack
  451.  * frame but also the old context we were working in (which is on the
  452.  * system call stack -- see handle_sig).
  453.  * The "valid_return" variable is 0 in the first case, 1 in the second.
  454.  */
  455.  
  456. short valid_return;
  457.  
  458. long ARGS_ON_STACK
  459. p_sigreturn()
  460. {
  461.     CONTEXT *oldctxt;
  462.     long *frame;
  463.     long sig;
  464.  
  465.     unwound_stack = 0;
  466. top:
  467.     frame = (long *)curproc->sysstack;
  468.     frame++;    /* frame should point at FRAME_MAGIC, now */
  469.     sig = frame[2];
  470.     if (*frame != FRAME_MAGIC || (sig < 0) || (sig >= NSIG)) {
  471.         FATAL("Psigreturn: system stack corrupted");
  472.     }
  473.     if (frame[1] == 0) {
  474.         DEBUG(("Psigreturn: frame at %lx points to 0", frame-1));
  475.         return 0;
  476.     }
  477.     unwound_stack = curproc->sysstack;
  478.     TRACE(("Psigreturn(%d)", (int)sig));
  479.  
  480.     curproc->sysstack = frame[1];    /* restore frame */
  481.     curproc->sigmask &= ~(1L<<sig); /* unblock signal */
  482.  
  483.     if (!valid_return) {
  484. /* here, the user is telling us that a longjmp out of a signal handler is
  485.  * about to occur; so we should unwind *all* the signal frames
  486.  */
  487.         goto top;
  488.     }
  489.     else {
  490.         valid_return = 0;
  491.         oldctxt = ((CONTEXT *)(&frame[2])) + 2;
  492.         if (oldctxt->regs[0] != CTXT_MAGIC) {
  493.             FATAL("p_sigreturn: corrupted context");
  494.         }
  495.         assert(curproc->magic == CTXT_MAGIC);
  496.         restore_context(oldctxt);
  497.         return 0;    /* dummy -- this isn't reached */
  498.     }
  499. }
  500.  
  501. /*
  502.  * stop a process because of signal "sig"
  503.  */
  504.  
  505. void
  506. stop(sig)
  507.     int sig;
  508. {
  509.     unsigned int code;
  510.     unsigned long oldmask;
  511.     PROC *p;
  512.  
  513.     code = sig << 8;
  514.  
  515.     if (curproc->pid == 0) {
  516.         FORCE("attempt to stop MiNT");
  517.         return;
  518.     }
  519.  
  520. /* notify parent */
  521.     if (curproc->ptracer) {
  522.         p = curproc->ptracer;
  523.         post_sig(p, SIGCHLD);
  524.     } else {
  525.         p = pid2proc(curproc->ppid);
  526.         if (p && !(p->sigflags[SIGCHLD] & SA_NOCLDSTOP))
  527.             post_sig(p, SIGCHLD);
  528.     }
  529.  
  530.     oldmask = curproc->sigmask;
  531.  
  532.     if ((1L << sig) & STOPSIGS) {
  533.         /* mask out most signals */
  534.         curproc->sigmask |= ~(UNMASKABLE | SIGTERM);
  535.     }
  536.     else
  537.         assert(curproc->ptracer);
  538.  
  539. /* sleep until someone signals us awake */
  540.     sleep(STOP_Q, (long) code | 0177);
  541.  
  542. /* when we wake up, restore the signal mask */
  543.     curproc->sigmask = oldmask;
  544.  
  545. /* and discard any signals that would cause us to stop again */
  546.     curproc->sigpending &= ~STOPSIGS;
  547. }
  548.  
  549. /*
  550.  * interrupt handlers to raise SIGBUS, SIGSEGV, etc. Note that for
  551.  * really fatal errors we reset the handler to SIG_DFL, so that
  552.  * a second such error kills us
  553.  */
  554.  
  555. void
  556. exception(sig)
  557.     int sig;
  558. {
  559.     curproc->sigflags[sig] |= SA_RESET;
  560.     DEBUG(("exception #%d raised", sig));
  561.     raise(sig);
  562. }
  563.  
  564. void
  565. sigbus()
  566. {
  567.     if (curproc->sighandle[SIGBUS] == SIG_DFL)
  568.         report_buserr();
  569.     exception(SIGBUS);
  570. }
  571.  
  572. void
  573. sigaddr()
  574. {
  575.     exception(SIGSEGV);
  576. }
  577.  
  578. void
  579. sigill()
  580. {
  581.     exception(SIGILL);
  582. }
  583.  
  584. void
  585. sigpriv()
  586. {
  587.     raise(SIGPRIV);
  588. }
  589.  
  590. void
  591. sigfpe()
  592. {
  593.     extern short fpu;    /* in main.c */
  594.     
  595.     if (fpu) {
  596.         CONTEXT *ctxt;
  597.  
  598.         ctxt = &curproc->ctxt[SYSCALL];
  599.  
  600.     /* 0x1f38 is a Motorola magic cookie to detect a 68882 idle state frame */
  601.         if (*(ushort *)ctxt->fstate == 0x1f38 && 
  602.             (ctxt->sfmt & 0xfff) >= 0xc0L && (ctxt->sfmt & 0xfff) <= 0xd8L) {
  603.             /* fix a bug in the 68882 - Motorola call it a feature :-) */
  604.             ctxt->fstate[ctxt->fstate[1]] |= 1 << 3;
  605.         }
  606.     }
  607.     raise(SIGFPE);
  608. }
  609.  
  610. void
  611. sigtrap()
  612. {
  613.     raise(SIGTRAP);
  614. }
  615.  
  616. void
  617. haltformat()
  618. {
  619.     FATAL("halt: invalid stack frame format");
  620. }
  621.  
  622. void
  623. haltcpv()
  624. {
  625.     FATAL("halt: coprocessor protocol violation");
  626. }
  627.